package com.quik.web.user.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.quik.common.Constants;
import com.quik.entity.SysAccount;
import com.quik.entity.SysRoleRight;
import com.quik.exception.ErrorCode;
import com.quik.web.user.service.AccountService;

import java.util.List;

/**
 * 功能说明：
 * 	        用户登录
 * @author jenny
 *
 */
@Controller
@RequestMapping("/login")
public class LoginController {
	private static Logger log = (Logger)Logger.getLogger(LoginController.class);
	
	@Autowired
	private AccountService accountService;
	
	/**
	 * 登录初始化
	 * @return
	 */
	@RequestMapping(value = "init")
	public ModelAndView init(){
		ModelAndView mav = new ModelAndView("/login/login");
		
		try{
			List classesList = accountService.findClassesCode();
			
			mav.addObject("classesList", classesList);
		}catch(Exception e){
			e.printStackTrace();
			log.error("LoginController init error: " + e.getMessage());
		}
		
		return mav;
	}
	
	/**
	 * 登录
	 * @param loginUser
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public ModelAndView login(String loginUser, String password, 
			String classes, HttpServletRequest request){
		ModelAndView mav = new ModelAndView("redirect:/main/main");
		
		try{
			SysAccount account = accountService.login(loginUser, password);
			if(account == null){
				mav.addObject("error_info", ErrorCode.LOGIN_ERROR);
				mav.setViewName("/login/login");
				request.setAttribute("error_info", ErrorCode.LOGIN_ERROR);
			}else{
				account.setClasses(classes);
				request.getSession().setAttribute(Constants.SESSION_ACCOUNT, account);
				List<SysRoleRight> rights = this.accountService.findRoleRightByUser(account);
				request.getSession().setAttribute(Constants.SESSION_AC, rights);
			}	
		}catch(Exception e){
			e.printStackTrace();
			log.error("LoginController login error: " + e.getMessage());
			mav.addObject("error_info", ErrorCode.SYSTEM_ERROR);
			mav.setViewName("/login/login");
		}
		
		return mav;
	}
	
	/**
	 * 登录管理员操作界面
	 * @param loginUser
	 * @param password
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "manageLogin", method = RequestMethod.POST)
	public ModelAndView manageLogin(String loginUser, String password, HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/login/index");
		
		try{
			SysAccount account = accountService.login(loginUser, password);
			if(account == null){
				mav.addObject("error_info", ErrorCode.LOGIN_ERROR);
				mav.setViewName("/login/login");
				request.setAttribute("error_info", ErrorCode.LOGIN_ERROR);
			}else{
				request.getSession().setAttribute(Constants.SESSION_ACCOUNT, account);
			}	
		}catch(Exception e){
			e.printStackTrace();
			log.error("LoginController login error: " + e.getMessage());
			mav.addObject("error_info", ErrorCode.SYSTEM_ERROR);
			mav.setViewName("/login/login");
		}
		
		return mav;
	}
	
	/**
	 * 菜单
	 *     根据权限自动生成，后期完成
	 * @return
	 */
	@RequestMapping(value = "menu", method = RequestMethod.GET)
	public ModelAndView menu(){
		ModelAndView mav = new ModelAndView("/login/leftmenu");
		return mav;
	}
	
	/**
	 * 退出系统
	 * @return
	 */
	@RequestMapping(value = "logout", method = RequestMethod.POST)
	public ModelAndView logout(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/login/login");
		
		request.getSession().removeAttribute(Constants.SESSION_ACCOUNT);
		mav.setViewName("redirect:/login/init");
		
		return mav;
	}
}
